package com.gsd.security.security;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TokenManager {

    //token 有效时常
    private long tokenEcpiration=24*60*60*10;
    //编码密钥
    private String tokenSignKey="123456";

    //1根据用户名使用jwt生成token
    public String createToken(String username){
        String token= Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis()+tokenEcpiration))
                .signWith(SignatureAlgorithm.HS512,
                        tokenSignKey).compressWith(CompressionCodecs.GZIP).compact();
        return token;
    }

    //2根据token字符串得到用户信息
    public String getUserInfoFromToken(String token){
        String userinfo =Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().getSubject();
        return userinfo;
    }

    //3删除token
    public void removeToken(String token){

    }

}
